package com.qymerge.manage.data.service.impl;

import com.qymerge.manage.base.dao.IProductDao;
import com.qymerge.manage.base.dao.IUserWXDao;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.data.dao.IYyueMgrDao;
import com.qymerge.manage.data.dto.YyueMgrDto;
import com.qymerge.manage.data.dto.export.YyueMgrExportDTO;
import com.qymerge.manage.data.ro.YyueMgrRO;
import com.qymerge.manage.data.service.IYyueMgrService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: YyueMgrServiceImpl</p>
 *<p>Description: 预约库存</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 18:18
 */
@Service
public class YyueMgrServiceImpl implements IYyueMgrService {

    @Autowired
    private IYyueMgrDao yyueMgrDao;

    @Autowired
    private IUserWXDao userWXDao;

    @Autowired
    private IProductDao productDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(YyueMgrDto ro, PageRequest pageRequest) {
        List<YyueMgrRO> list = yyueMgrDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = yyueMgrDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public YyueMgrRO detail(Long id) {
        YyueMgrRO xfRecordRO = yyueMgrDao.findOne(id);
        return xfRecordRO;
    }

    /**
     * 新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(YyueMgrRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = yyueMgrDao.install(ro);
        } else {
            rn = yyueMgrDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = yyueMgrDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    /**
     * 导出表格
     *
     * @param ro
     * @return
     */
    @Override
    public List<YyueMgrExportDTO> exportData(YyueMgrDto ro) {
        List<YyueMgrExportDTO> list = new ArrayList<YyueMgrExportDTO>();
        YyueMgrExportDTO dto = null;

        List<YyueMgrRO> yyueMgrROS = yyueMgrDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(yyueMgrROS)) {
            for (YyueMgrRO yyueMgrRO : yyueMgrROS) {
                dto = new YyueMgrExportDTO();

                dto.setId(yyueMgrRO.getId());
                dto.setYdate(yyueMgrRO.getYdate());
                dto.setYtime(yyueMgrRO.getYtime());
                dto.setNum(yyueMgrRO.getNum());
                dto.setHavenum(yyueMgrRO.getHavenum());

                list.add(dto);
            }
        }

        return list;
    }

}
